const msg = require('../database/tables/msg')
const user = require('../database/tables/user')
const { sequelize } = require('../database/init')
const rp = require('request-promise')
const { formatBody } = require('../tools')

module.exports = {
  // 获取用户列表
  async list (ctx) {
    console.log('ctx.request.body', ctx.request.body)
    let { limit, page, uid } = ctx.request.body
    limit = limit || 20
    page = page || 1
    let offset = (page - 1) * limit
    let list = await user.findAll({
      limit,
      offset,
      attributes: ['uid', 'nickname', 'avatar', 'foul_num'],
      order: [
        ['createdAt', 'DESC']
      ]
    })
    return ctx.body = formatBody(1, 'ok', list)
  },
  async foul (ctx) {
    let { uid, disable_time } = ctx.request.body
    let user = user.findOne({
      where: {
        uid
      }
    })
    disable_time = disable_time ? disable_time : 1000 * 60
    if (!user) {
      return ctx.body = formatBody(0, '用户不存在')
    } else {
      await user.update({
        foul_num: 20,
        disable_time: new Date().getTime() + '' + disable_time
      }, {
        where: { uid }
      })
      return ctx.body = formatBody(1, '禁言成功')
    }
  }
}
